Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-querybuilder
Advanced tools
The Query Builder component for React
React Query Builder is a fully customizable query builder component for React, along with a collection of utility functions for importing from, and exporting to, various query languages like SQL, MongoDB, and more.
Complete documentation is available at react-querybuilder.js.org.
Click here to see a live, interactive demo.
To use the official compatibility components as seen in the demo, take a look at the packages under the @react-querybuilder
org on npmjs.com. We currently support:
npm install react-querybuilder --save
# OR
yarn add react-querybuilder
import { useState } from 'react';
import { Field, QueryBuilder, RuleGroupType } from 'react-querybuilder';
import 'react-querybuilder/dist/query-builder.css';
const fields: Field[] = [
{ name: 'firstName', label: 'First Name' },
{ name: 'lastName', label: 'Last Name' },
{ name: 'age', label: 'Age', inputType: 'number' },
{ name: 'address', label: 'Address' },
{ name: 'phone', label: 'Phone' },
{ name: 'email', label: 'Email', validator: ({ value }) => /^[^@]+@[^@]+/.test(value) },
{ name: 'twitter', label: 'Twitter' },
{ name: 'isDev', label: 'Is a Developer?', valueEditorType: 'checkbox', defaultValue: false },
];
const initialQuery: RuleGroupType = {
combinator: 'and',
rules: [],
};
export const App = () => {
const [query, setQuery] = useState(initialQuery);
return <QueryBuilder fields={fields} query={query} onQueryChange={q => setQuery(q)} />;
};
To enable drag-and-drop functionality, install the @react-querybuilder/dnd
package and nest <QueryBuilder />
under <QueryBuilderDnD />
.
To export queries as SQL, MongoDB, or one of several other formats, use the formatQuery
function.
const query = {
combinator: 'and',
rules: [
{
field: 'first_name',
operator: 'beginsWith',
value: 'Stev',
},
{
field: 'last_name',
operator: 'in',
value: 'Vai, Vaughan',
},
],
};
const sqlWhere = formatQuery(query, 'sql');
console.log(sqlWhere);
/*
`(first_name like 'Stev%' and last_name in ('Vai', 'Vaughan'))`
*/
To import queries use parseSQL
, parseCEL
, parseJsonLogic
, or parseMongoDB
depending on the source.
Tip: parseSQL
will accept a full SELECT
statement or the WHERE
clause by itself (everything but the expressions in the WHERE
clause will be ignored). Trailing semicolon is optional.
const query = parseSQL(
`SELECT * FROM my_table WHERE first_name LIKE 'Stev%' AND last_name in ('Vai', 'Vaughan')`
);
console.log(query);
/*
{
"combinator": "and",
"rules": [
{
"field": "first_name",
"operator": "beginsWith",
"value": "Stev",
},
{
"field": "last_name",
"operator": "in",
"value": "Vai, Vaughan",
},
],
}
*/
Note: formatQuery
and the parse*
functions can be used without importing React (e.g., on the server) like this:
import { formatQuery } from 'react-querybuilder/dist/formatQuery';
import { parseCEL } from 'react-querybuilder/dist/parseCEL';
import { parseJsonLogic } from 'react-querybuilder/dist/parseJsonLogic';
import { parseMongoDB } from 'react-querybuilder/dist/parseMongoDB';
import { parseSQL } from 'react-querybuilder/dist/parseSQL';
[v5.4.1] - 2023-01-30
parseJsonLogic
output when a negated "between", "in", "contains", "beginsWith", or "endsWith" rule is the only operation.FAQs
React Query Builder component for constructing queries and filters, with utilities for executing them in various database and evaluation contexts
The npm package react-querybuilder receives a total of 42,101 weekly downloads. As such, react-querybuilder popularity was classified as popular.
We found that react-querybuilder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.